package com.zdy.im.user.controller;



import java.util.List;

import com.sixeco.framework.common.api.ApiDoc;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.SysResultCodeEnum;
import com.sixeco.framework.core.db.IterableConverter;
import com.sixeco.framework.http.data.vo.HttpRequestByIdVo;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.http.handler.OutputResponse;
import com.zdy.im.user.UrlCommand;
import com.zdy.im.user.dto.request.ImRoomChatMessageRequestDto;
import com.zdy.im.user.dto.response.ImRoomChatMessageResponseDto;
import com.zdy.im.user.dto.response.ImRoomInfoResponseDto;
import com.zdy.im.user.facade.ImRoomInfoFacade;
import com.zdy.im.user.service.ImRoomChatMessageService;
import com.zdy.im.user.service.ImRoomInfoService;
import com.zdy.im.user.vo.request.*;
import com.zdy.im.user.vo.response.ImRoomChatMessageShowResponseVo;
import com.zdy.im.user.vo.response.ImRoomInfoShowResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Title: ImRoomInfoController.java
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0
 *  <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 * @Description: TODO(说明) 对象缓存统一模板实现类;
 */
@ApiDoc(
        module = "群信息表模块", isApp = true
)
@Controller
public class ImRoomInfoController {

    @Autowired
    private ImRoomInfoFacade imRoomInfoFacade;

    @Autowired
    private ImRoomInfoService imRoomInfoService;

    @Autowired
    private ImRoomChatMessageService imRoomChatMessageService;



    /**
    * @Title: test测试生成类分页信息
    * @Description:testRequestVo @{Link TestRequestVo}
    * @param
    * @return
    * @throw
    * @author suven
    * @date 2019-07-19 13:16:48
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ApiDoc(
            value = "分页获取群信息表信息",
            request = ImRoomInfoQueryRequestVo.class,
            response = ImRoomInfoShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.user_imRoomInfo_pageList, method = RequestMethod.GET)
    public void list(OutputResponse out, ImRoomInfoQueryRequestVo imRoomInfoQueryRequestVo){
        if (imRoomInfoQueryRequestVo.getMasterId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        ResponseResultList list = imRoomInfoFacade.list(imRoomInfoQueryRequestVo);
        out.write(list);
    }





    /**
    * @Title: 查看test测试生成类信息
    * @Description:testRequestVo @{Link TestRequestVo}
    * @param
    * @return
    * @throw
    * @author suven
    * @date 2019-07-19 13:16:48
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ApiDoc(
            value = "查看群信息表信息",
            request = HttpRequestByIdVo.class,
            response = ImRoomInfoShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.user_imRoomInfo_info ,method = RequestMethod.GET)
    public void detail(OutputResponse out, HttpRequestByIdVo idRequestVo){

            ImRoomInfoResponseDto imRoomInfoResponseDto = imRoomInfoService.getImRoomInfoById(idRequestVo.getId());
            ImRoomInfoShowResponseVo vo =  ImRoomInfoShowResponseVo.build().clone(imRoomInfoResponseDto);
        out.write(vo);
    }

    @ApiDoc(
            value = "创建群",
            request = RoomInfoAddRequestVo.class,
            response = ImRoomInfoResponseDto.class
    )
    @RequestMapping(value = UrlCommand.user_imRoomInfo_add ,method = RequestMethod.POST)
    public void save(OutputResponse out, RoomInfoAddRequestVo roomInfoAddRequestVo){
        if (roomInfoAddRequestVo.getMasterId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        ImRoomInfoResponseDto res = imRoomInfoFacade.add(roomInfoAddRequestVo);
        out.write(res);
    }


    @ApiDoc(
            value = "修改群头像",
            request = RoomImgRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_imRoomInfo_upHeadImg,method = RequestMethod.POST)
    public  void  upRoomHeadImg(OutputResponse out, RoomImgRequestVo roomImgRequestVo){
        if (roomImgRequestVo.getRoomId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = imRoomInfoFacade.upRoomImg(roomImgRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "修改群公告/简介/名称/禁言设置",
            request = RoomInfoRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_imRoomInfo_upNoticeRemarks,method = RequestMethod.POST)
    public  void  upRoomHeadImg(OutputResponse out, RoomInfoRequestVo roomInfoRequestVo){
        if (roomInfoRequestVo.getRoomId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = imRoomInfoFacade.upRoomInfo(roomInfoRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "解散群",
            request = RoomRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_imroomInfo_delRoom,method = RequestMethod.POST)
    public  void  del(OutputResponse out, RoomRequestVo roomRequestVo){
        if (roomRequestVo.getRoomId() == 0 ) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = imRoomInfoFacade.delRoom(roomRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "获取群消息历史列表--根据群id和已获取的消息最大id,分页获取信息",
            request = ImRoomChatMessageHistoryRequestVo.class,
            response = ImRoomChatMessageShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.USER_ROOM_CHAT_MESSAGE_LIST, method = RequestMethod.GET)
    public void list(OutputResponse out, ImRoomChatMessageHistoryRequestVo messageHistoryRequestVo){
        ResponseResultList list =  ResponseResultList.build();
        ImRoomChatMessageRequestDto imRoomChatMessageRequestDto = ImRoomChatMessageRequestDto.build( ).clone(messageHistoryRequestVo);

        BasePage page =  BasePage.build().toPageSize(messageHistoryRequestVo.getPageSize()).toPageNo(messageHistoryRequestVo.getPageNo());
        page.toParamObject(imRoomChatMessageRequestDto );
        ResponseResultList<ImRoomChatMessageResponseDto> resultList = imRoomChatMessageService.getImRoomChatMessageByRoomIdPage(page);

        if(null == resultList || resultList.getList().isEmpty() ){
            out.write(list);
            return;
        }
        List<ImRoomChatMessageShowResponseVo> listVo = IterableConverter.convertList(resultList.getList(),ImRoomChatMessageShowResponseVo.class);

        list =  ResponseResultList.build().toList(listVo)
                .toIsNextPage(resultList.getIsNextPage());

        out.write(list);
    }

    @ApiDoc(
            value = "查看群对应的消息详细信息",
            request = HttpRequestByIdVo.class,
            response = ImRoomChatMessageShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.USER_ROOM_CHAT_MESSAGE_INFO ,method = RequestMethod.GET)
    public void roomDetailByRoomId(OutputResponse out, HttpRequestByIdVo idRequestVo){

        ImRoomChatMessageResponseDto imRoomChatMessageResponseDto = imRoomChatMessageService.getImRoomChatMessageById(idRequestVo.getId());
        ImRoomChatMessageShowResponseVo vo =  ImRoomChatMessageShowResponseVo.build().clone(imRoomChatMessageResponseDto);
        out.write(vo);
    }
}
